const ShopModel = require('../models/ShopModel')
const CarModel = require('../models/CarModel')

const shopService = {
  getShopByAddress({ province, city, type_id }) {
    return ShopModel.find({
      province,
      city,
    }).populate({
      path: 'carList',
      match: {
        type_id,
        is_rend: 0,
      },
    })
  },
  getShop() {
    return ShopModel.find()
  },
  addShop(data) {
    return ShopModel.create(data)
  },
  updateShop(_id, data) {
    return ShopModel.updateOne(
      {
        _id,
      },
      {
        $set: data,
      }
    )
  },
  async deleteShop(_id) {
    await CarModel.deleteMany({
      shop_id: _id,
    }).exec()
    return ShopModel.deleteOne({
      _id,
    })
  },
}

module.exports = shopService
